<?php
global $current_user, $hide_right_sidebar;
$hide_right_sidebar = true;
$tilbake = 'Avbryt';
if (isset($_POST['framodul'])) {
    $framodul = $_POST['framodul'];
} else {
    $framodul = '/';
}
$h = array('&lt;', '&gt;');
$s = array('<', '>');
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) {
    switch ($_POST['action']){
        case 'Send':
            $mottaker = str_replace($h, $s, $_POST['mottaker']);
            $emne = $_POST['emne'];
            $melding = $_POST['melding'];
            $avsender = str_replace($h, $s, $_POST['avsender']);
            $headers = "From: $avsender";
            if (empty($_POST['mottaker']) || empty($_POST['emne']) || empty($_POST['melding'])) {
                $error = 'Ett eller flere av feltene er tomme.</br>Kan ikke sende epost.';
                break;
            }

            if (mail($mottaker, $emne, $melding, $headers)) {
                echo 'Eposten er sendt';
                $tilbake = 'Tilbake';
            } else {
                $error = 'Det har oppstått en feil under sendingen av eposten.</br>Eposten er ikke sendt.';
                $tilbake = 'Gi opp';
            }
            break;
        case 'Lagre':
            $mottaker = str_replace($h, $s, $_POST['mottaker']);
            $emne = $_POST['emne'];
            $melding = $_POST['melding'];
            $avsender = str_replace($h, $s, $_POST['avsender']);
            break;
        case 'Send epost':
            if (!isset($_POST['til_alle'])) {
                if (isset($_POST['idnum'])) {
                    $mottaker = get_userdata($_POST['idnum']);
                    $mottaker = "$mottaker->first_name $mottaker->last_name <$mottaker->user_email>";
                } else {
                    $mottaker = '';
                }
            } else {
                $mottaker = '';
                $firstrun = true;
                foreach (get_users_of_blog() as $mot) {
                    $mot = get_userdata($mot->ID);
                    if ($firstrun)
                        $firstrun = false;
                    else
                        $mottaker .= ', ';
                    $mottaker .= "$mot->first_name $mot->last_name <$mot->user_email>";
                }
            }
            $avsender = get_userdata($current_user->ID);
            $avsender = "$avsender->first_name $avsender->last_name <$avsender->user_email>";
            break;
        default:
            echo 'Ukjent kommando';
            break;
    }
}
?>

<div id="epost">
    <?php
    if (isset($error)) echo '<p class="error">' . $error . '</p>';
    ?>
    <form class="adminButtons" method="post" action="<?php the_permalink() ?>">
        <table>
            <tr>
                <td>
                    <label for="mottaker">Mottaker(e): </label>
                </td>
                <td>
                    <input name="mottaker" type="text" value="<?php echo str_replace($s, $h, $mottaker) ?>" style="width:50%;"/>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="emne">Emne: </label>
                </td>
                <td>
                    <input name="emne" type="text" value="<?php echo $emne ?>" style="width:50%;"/>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="avsender">Avsender: </label>
                </td>
                <td>
                    <a name="avsender"><?php echo str_replace($s, $h, $avsender) ?></a>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="melding">Melding: </label>
                </td>
                <td>
                    <textarea name="melding" rows="20" cols="70"><?php echo $melding ?></textarea>
                </td>
            </tr>
        </table>
        <input name="action" type="submit" value="Send"/>
        <input name="action" type="submit" value="Lagre"/>
        <input name="til-side" type="hidden" value="<?php echo $_POST['til-side'] ?>"/>
        <input name="idnum" type="hidden" value="<?php echo $_POST['idnum'] ?>"/>
        <input name="framodul" type="hidden" value="<?php echo $framodul ?>"/>
        <input name="avsender" type="hidden" value="<?php echo $avsender ?>"/>
    </form>
    <form class="adminButtons" method="post" action="<?php echo $framodul ?>">
        <input name="action" type="submit" value="<?php echo $tilbake ?>"/>
        <input name="til-side" type="hidden" value="<?php echo $_POST['til-side'] ?>"/>
        <input name="idnum" type="hidden" value="<?php echo $_POST['idnum'] ?>"/>
    </form>
</div>